package com.blensmile.network;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Blensmile on 2017/3/16.
 */

public class RestClient {
//    public static final String BASE_URL = "http://api.200h.com";
    //public static final String BASE_URL = "https://api.talkmate.com";  //online
    public static final String BASE_URL = "http://10.0.1.217:3001/";

    public static final String API_VERSION = "4.1";
//    public static final String APP_INFO = "Android " + AppConfiguration.getDevName() + " " + AppConfiguration.getAppVer();
    private static ApiService apiService;
    private static OkHttpClient client;

    static {
        setupRestClient();
    }

    /**
     * Constructor
     */
    private RestClient() {
    }

//    private RequestInterceptor requestInterceptor = new RequestInterceptor() {
//        @Override
//        public void intercept(RequestFacade request) {
//
//            String userAgent = "Talkmate:";
//            userAgent = userAgent + "1.0" + "-" + "Phone Model:" + "xxx" + "-" + "Android Version:" + "4.4.2";
//            request.addHeader("User-Agent", userAgent);
//
//        }
//    };


    private static void setupRestClient() {
        Gson gson = new GsonBuilder()
                .create();
        client = new OkHttpClient();
//        client.setConnectTimeout(10, TimeUnit.SECONDS);
        HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
        logger.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
               .connectTimeout(10,TimeUnit.SECONDS)
               .readTimeout(10,TimeUnit.SECONDS)
               .writeTimeout(10,TimeUnit.SECONDS)
               .addNetworkInterceptor(logger)
               .build();

        client.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                HttpUrl url = request.url().newBuilder()
                        .addQueryParameter("api-version", API_VERSION)
//                        .addQueryParameter("user-agent", APP_INFO)
                        .build();
                request = request.newBuilder().url(url).build();
                Response response = chain.proceed(request);
                return response;
            }
        });
//        OkClient okclient = new OkClient(client);
//        RequestInterceptor requestInterceptor = new RequestInterceptor() {
//            @Override
//            public void intercept(RequestFacade request) {
//                request.addHeader("api-version", API_VERSION);
//                request.addHeader("user-agent", APP_INFO);
//            }
//        };
//        RestAdapter restAdapter = new RestAdapter.Builder()
//                .setLogLevel(RestAdapter.LogLevel.FULL)
//                .setClient(okclient)
//                .setEndpoint(BASE_URL)
//                .setConverter(new GsonConverter(gson))
//                .setErrorHandler(new ErroHandle())
//                .setRequestInterceptor(requestInterceptor)
//                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(new CallAdapter.Factory() {
                    @Override
                    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
                        return null;
                    }
                })
                .client(okHttpClient)
                .build();

        apiService = retrofit.create(ApiService.class);

//        apiService = restAdapter.create(ApiService.class);

    }

    /**
     * @return apiService
     */
    public static ApiService getApiService() {
        return apiService;
    }

}